import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:tiktokadult/assets_tool/images.dart';
import 'package:tiktokadult/hj_page/mine_business/mine_vip/coupon_model.dart';
import 'package:tiktokadult/hj_utils/api_service/mine_service.dart';
import 'package:tiktokadult/hj_utils/widget_utils.dart';
import 'package:tiktokadult/tools_base/loading/l_error_widget.dart';
import 'package:tiktokadult/tools_base/loading/loading_center_widget.dart';
import 'package:tiktokadult/tools_base/refresh/pull_refresh.dart';

class AICouponsBottom extends StatefulWidget {
  const AICouponsBottom({super.key});

  @override
  State<AICouponsBottom> createState() => _AICouponsBottomState();
}

class _AICouponsBottomState extends State<AICouponsBottom> {
  int currentPage = 1;
  List<AICouponModel>? dataList;
  RefreshController? refreshCtr;

  @override
  void initState() {
    super.initState();
    loadData();
  }

  loadData({int page = 1}) async {
    dataList ??= [];
    final res = await MineService.backPack(currentPage, limit: 10);
    if (res != null) {
      if (page == 1) dataList?.clear();
      dataList?.addAll(res);
      currentPage = page;
    }

    setState(() {});
    refreshCtr?.refreshCompleted();
    (res?.length ?? 0) < 10 ? refreshCtr?.loadNoData() : refreshCtr?.loadComplete();
  }

  loadMoreData() => loadData(page: currentPage + 1);

  @override
  Widget build(BuildContext context) {
    return Container(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 18),
        decoration: BoxDecoration(
          color: Color(0xff030F18),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(20),
            topRight: Radius.circular(20),
          ),
        ),
        child: () {
          return Column(
            children: [
              Container(
                width: 36,
                height: 5,
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(.2),
                  borderRadius: BorderRadius.circular(100),
                ),
              ),
              18.sizeBoxH,
              Text(
                '使用抵扣券',
                style: textStyle(18, Colors.white.withOpacity(.9), FontWeight.w600),
              ),
              18.sizeBoxH,
              Expanded(
                child: () {
                  if (dataList == null) return LoadingCenterWidget();
                  if (dataList?.isEmpty == true)
                    return CErrorWidget(
                      retryOnTap: loadData,
                    );
                  return pullYsRefresh(
                    onInit: (ctr) => refreshCtr = ctr,
                    onRefresh: (ctr) => loadData(),
                    child: ListView.separated(
                      itemBuilder: (context, index) {
                        AICouponModel? model = dataList?[index];
                        return InkWell(
                          onTap: () => Get.back(result: model),
                          child: Container(
                            alignment: Alignment.center,
                            height: 68,
                            decoration: BoxDecoration(
                              image: DecorationImage(
                                image: AssetImage('ai_coupon_bg.webp'.aiPath),
                                fit: BoxFit.fill,
                              ),
                            ),
                            child: Row(
                              children: [
                                18.sizeBoxW,
                                Text(
                                  '${model?.goodsName}',
                                  style: textStyle(18, Color(0xff7E4444), FontWeight.w600),
                                ),
                                12.sizeBoxW,
                                Expanded(
                                  child: Text(
                                    '${model?.goodsDesc}',
                                    style: textStyle(14, Color(0xff7E4444), FontWeight.w400),
                                    maxLines: 1,
                                    overflow: TextOverflow.ellipsis,
                                  ),
                                ),
                                12.sizeBoxW,
                                Container(
                                  padding: EdgeInsets.symmetric(horizontal: 12),
                                  alignment: Alignment.center,
                                  decoration: BoxDecoration(
                                    color: Color(0xffFFDCB3),
                                    borderRadius: BorderRadius.circular(22),
                                  ),
                                  height: 26,
                                  child: Text(
                                    '立即使用',
                                    style: textStyle(12, Color(0xff7E4444), FontWeight.w400),
                                  ),
                                ),
                                18.sizeBoxW,
                              ],
                            ),
                          ),
                        );
                      },
                      separatorBuilder: (context, index) => 12.sizeBoxH,
                      itemCount: dataList?.length ?? 0,
                    ),
                  );
                }(),
              )
            ],
          );
        }());
  }
}
